<?php

namespace App\Models;

use App\Http\Controllers\PaymentController;
use App\Http\Controllers\WxPayController;
use App\Util\Url;
use Illuminate\Database\Eloquent\Model;

class Reward extends Model
{
    protected $table = "c_rewards";


    public static function List()
    {
        $list = self::OrderBy("id","asc")->get();
        if($list){
            $list = $list->toArray();
            foreach ($list as $k=>&$value){
                $value['image'] = Url::getHttpHost().'upload/'.$value['image'];
            }
            return $list;
        }
        return [];
    }




    /**
     * @param $data
     * 打赏
     */
    public function addReward($data)
    {

        $store_order = new StoreOrder();
        if($data['type'] ==10){ //医生打赏
            $doc = (new Doctor())->where([['id','=',$data['doctor_id']]])->first();
            $store_order->work_id = $doc->work_id;
        }elseif ($data['type'] ==20){ //工作室打赏
            $store_order->work_id = $data['doctor_id'];
        }
        $store_order->money = $data['money'];
        $store_order->user_id = $data['user_id'];
        $store_order->store_id = $data['doctor_id'];
        $store_order->type_id = isset($data['type_id'])?$data['type_id']:0;
        $store_order->type = $data['type'];
        $store_order->name = $data['name'];
        $store_order->sn = (new Store())->storeOrderNumber();
        $store_order->save();
        return ['order_id'=>$store_order->id];
    }

    /****
     * @param $order_id
     * @return mixed
     * 打赏详情
     */
    public static function getRewardDetail($order_id)
    {
        return self::where([['id', '=', $order_id]])->first();
    }


    /**
     * @param $res
     * @return array|bool|string
     * 打赏订单支付
     */
    public function pay($res)
    {
        $store_order = (new StoreOrder())->where([['id','=',$res['order_id']]])->first();
        if($store_order->user_id != $res['user_id']){
            return false;
        }
        $store_order->pay_type = $res['pay_type'];
        $store_order->save();
        if($res['pay_type'] ==1){  //微信支付
            $data['sn'] = $store_order->sn;
            $data['des'] = '100_益鹿出院-打赏订单支付';
            $data['price'] = $store_order->money;
//            if($store_order->money == 0){
//                $store_order_dt = new StoreOrder();
//                $lit['order_status'] = 1;
//                $lit['pay_status'] = 1;
//                $lit['pay_time'] = date("Y-m-d H:i:s", time());
//                $store_order_dt->where([['id', '=', $res['store_order_id']]])->update($lit);
//                (new Store())->where([['id','=',$store_order->store_id]])->update(['topping' => $store_order->type, 'buy_type' => $store_order->type_id, "is_online" => 1]);
//                return ["result"=>"success"];
//            }
            $res = (new WxPayController())::setPay($data);
            return $res;
        }elseif ($res['pay_type'] ==2){  //支付宝
            $datas['sn'] = $store_order->sn;
            $datas['pay_price'] = $store_order->money;
            $datas['user_id'] = $store_order->user_id;
            $datas['subject'] = '100_益鹿出院-打赏订单支付';
//            if($pay_type->money == 0){
//                $store_order_dt = new StoreOrder();
//                $lit['order_status'] = 1;
//                $lit['pay_status'] = 1;
//                $lit['pay_time'] = date("Y-m-d H:i:s", time());
//                $store_order_dt->where([['id', '=', $res['store_order_id']]])->update($lit);
//                (new Store())->where([['id','=',$store_order->store_id]])->update(['topping' => $store_order->type, 'buy_type' => $store_order->type_id, "is_online" => 1]);
//                return "success";
//            }
            $res = (new PaymentController())->doPay($datas);
            return $res;
        }

    }





}
